package com.epfresh.fragment;


import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.epfresh.R;
import com.epfresh.activity.DetailGoodsActivity;
import com.epfresh.global.BaseFragment;
import com.epfresh.views.stick.OnStickyListener;
import com.epfresh.views.stick.ScrollWebView;


public class DcHomePageFragment extends BaseFragment implements OnStickyListener {
    private ScrollWebView webview;
    private ProgressBar pb;
    private String url;


    public void updateData(String url) {
        this.url = url;
        if (!TextUtils.isEmpty(url) && webview != null) {
            webview.loadUrl(url);
        }

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_webview_dc_homepage, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        pb = (ProgressBar) view.findViewById(R.id.pb);
        pb.setVisibility(View.GONE);
        webview = (ScrollWebView) view.findViewById(R.id.wv);
//        pb.setVisibility(View.VISIBLE);
        webViewSetting();
        if (!TextUtils.isEmpty(url)) {
            webview.loadUrl(url);
        }

    }


    public void webViewSetting() {
        WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);//启用js
        settings.setJavaScriptCanOpenWindowsAutomatically(true);//js和android交互
        settings.setPluginState(WebSettings.PluginState.ON);
//        settings.setAppCachePath(cacheDirPath); //设置缓存的指定路径
        settings.setAllowFileAccess(true); // 允许访问文件
        settings.setAppCacheEnabled(false); //设置H5的缓存打开,默认关闭
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setUseWideViewPort(true);//设置webview自适应屏幕大小
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);//设置，可能的话使所有列的宽度不超过屏幕宽度
        settings.setLoadWithOverviewMode(true);//设置webview自适应屏幕大小
        settings.setDomStorageEnabled(true);//设置可以使用localStorage
        settings.setSupportZoom(false);//关闭zoom按钮
        settings.setBuiltInZoomControls(false);//关闭zoom
//        webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress > 99) {
                    pb.setVisibility(View.GONE);
                } else {
                    if (pb.getVisibility() == View.GONE) {
//                        pb.setVisibility(View.VISIBLE);
                    }
                    pb.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                if (title != null && !"".equals(title)) {
//                    toolbarTitle.setText(title);
                }
            }
        });
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (url != null && url.startsWith("epfresh://product") && getActivity() != null) {
                    Intent intent = new Intent(getContext(), DetailGoodsActivity.class);
                    String id = Uri.parse(url).getQueryParameter("id");
                    intent.putExtra("id", id);
                    intent.putExtra("storeId",Uri.parse(url).getQueryParameter("storeId"));
                    startActivity(intent);
                    return true;
                } else {
                    view.loadUrl(url);
                }
                return false;
            }


            @Override
            public void onLoadResource(WebView view, String url) {

            }

            @Override
            public void onPageFinished(WebView view, String url) {
            }
        });

    }


//    public void bindData() {
//        WebView wv_content = getWebView();
//        wv_content.loadUrl("http://news.qq.com");
//        wv_content.setWebViewClient(new WebViewClient() {
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                view.loadUrl(url);
//                return true;
//            }
//        });
//    }

    @Override
    public String getBaseTag() {
        return null;
    }

    @Override
    public boolean isTop() {
        return webview != null && webview.isTop();
    }

    @Override
    public void setScroll(boolean canScroll) {

    }
}
